package nsalt.arch

import chisel3._
import chisel3.util._

import nsalt._

object RVAInstr extends InstrType {
  // Note: use instr(14,12) to distinguish D/W inst
  // def LR      = BitPat("b00010??00000_?????_???_?????_0101111")
  // def SC      = BitPat("b00011??00000_?????_???_?????_0101111")
  def LR_D    = BitPat("b00010_??_00000_?????_011_?????_0101111")
  def SC_D    = BitPat("b00011_??_?????_?????_011_?????_0101111")
  def LR_W    = BitPat("b00010_??_00000_?????_010_?????_0101111")
  def SC_W    = BitPat("b00011_??_?????_?????_010_?????_0101111")
  def AMOSWAP = BitPat("b00001_??_?????_?????_01?_?????_0101111")
  def AMOADD  = BitPat("b00000_??_?????_?????_01?_?????_0101111")
  def AMOXOR  = BitPat("b00100_??_?????_?????_01?_?????_0101111")
  def AMOAND  = BitPat("b01100_??_?????_?????_01?_?????_0101111")
  def AMOOR   = BitPat("b01000_??_?????_?????_01?_?????_0101111")
  def AMOMIN  = BitPat("b10000_??_?????_?????_01?_?????_0101111")
  def AMOMAX  = BitPat("b10100_??_?????_?????_01?_?????_0101111")
  def AMOMINU = BitPat("b11000_??_?????_?????_01?_?????_0101111")
  def AMOMAXU = BitPat("b11100_??_?????_?????_01?_?????_0101111")
  // funct3 === 010 or 011
  
  val table = Array(
    // LR          -> List(InstrI, FuncType.lsu, LoadStoreOperType.lr),
    LR_D        -> List(InstrI, FuncType.lsu, LoadStoreOperType.lr),
    LR_W        -> List(InstrI, FuncType.lsu, LoadStoreOperType.lr),
    // SC          -> List(InstrS, FuncType.lsu, LoadStoreOperType.sc),
    SC_D        -> List(InstrSA, FuncType.lsu, LoadStoreOperType.sc),
    SC_W        -> List(InstrSA, FuncType.lsu, LoadStoreOperType.sc),
    AMOSWAP     -> List(InstrR, FuncType.lsu, LoadStoreOperType.amoswap),
    AMOADD      -> List(InstrR, FuncType.lsu, LoadStoreOperType.amoadd),
    AMOXOR      -> List(InstrR, FuncType.lsu, LoadStoreOperType.amoxor),
    AMOAND      -> List(InstrR, FuncType.lsu, LoadStoreOperType.amoand),
    AMOOR       -> List(InstrR, FuncType.lsu, LoadStoreOperType.amoor),
    AMOMIN      -> List(InstrR, FuncType.lsu, LoadStoreOperType.amomin),
    AMOMAX      -> List(InstrR, FuncType.lsu, LoadStoreOperType.amomax),
    AMOMINU     -> List(InstrR, FuncType.lsu, LoadStoreOperType.amominu),
    AMOMAXU     -> List(InstrR, FuncType.lsu, LoadStoreOperType.amomaxu)
  )
}
